﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[Serializable]
public class SerializeConfig : IScriptableObject<SerializeConfig>
{
    public string tempPath = "Assets/Editor/Template/";
    public string prefabPath = "/Resources/Prefab/Panel";
    public string classPath = "/Scripts/CSharp/FaceCenter/Panel";
    public string luaPath = "/Lua/FaceCenter/Panel";

    public string ClassSuffix = "Panel";
    public bool FindGenerate = false;

    public Dictionary<Type, string> typeMap = new Dictionary<Type, string>()
    {
        {typeof(Transform),         "tf"  },
        {typeof(Animation),         "atn" },
        {typeof(Animator),          "atr" },
        {typeof(UIPanel),           "pnl" },
        {typeof(UILabel),           "txt" },
        {typeof(UISprite),          "sp"  },
        {typeof(UIWidget),          "wgt" },
        {typeof(UIButton),          "btn" },
        {typeof(UIToggle),          "tog" },
        {typeof(UIGrid),            "grid"},
        {typeof(UITable),           "tbl" },
        {typeof(UIInput),           "ipt" },
        {typeof(UISlider),          "sdr" },
        {typeof(UITweener),         "tnr" },
        {typeof(UITexture),         "tte" },
        {typeof(UIScrollView),      "slv" },
        {typeof(ParticleSystem) ,   "ps"  },
    };

    public string GetTypeString(Component com)
    {
        if (com != null)
        {
            Type type = com.GetType();
            if (typeMap.ContainsKey(type))
            {
                return typeMap[type];
            }
            return type.Name;
        }
        return string.Empty;
    }

    public string GetTemplateFile(bool script, bool find)
    {
        if (find)
        {
            return script ? "TempCS.txt" : "TempLua.txt";
        }
        return script ? "CreateCS.txt" : "CreateLua.txt";
    }

    public string GetScriptPath(bool script, out string scriptExt)
    {
        scriptExt = script ? "cs" : "lua";
        return script ? classPath : luaPath;
    }

    /// <summary>
    /// 批量生成查找代码
    /// </summary>
    /// <param name="script"></param>
    public void CollectPrefabRes(bool script)
    {
        string[] dirs = AssetDatabase.FindAssets("t:Prefab", new[] { prefabPath });
        string fullPath = GetScriptPath(script, out string scriptExt);
        int fileCount = 0; fullPath = Application.dataPath + fullPath;

        if (Directory.Exists(fullPath))
        {
            Directory.Delete(fullPath, true);
        }
        Directory.CreateDirectory(fullPath);

        for (int i = 0; i < dirs.Length; i++)
        {
            string path = AssetDatabase.GUIDToAssetPath(dirs[i]);
            var target = AssetDatabase.LoadAssetAtPath<GameObject>(path);

            ExpandToolsMenu.CreateWindow(target, script); fileCount++;
            EditorUtility.DisplayProgressBar("Create Script", path, (float)fileCount / dirs.Length);
            Debug.Log(path + " == " + fileCount);
        }

        EditorUtility.ClearProgressBar();
        AssetDatabase.Refresh();
    }
}